<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <RecordTree class="w-1/5 xl:w-1/6" @select="handleSelect" />
    <BasicTable @register="registerTable" class="w-4/5 xl:w-5/6" :searchInfo="searchInfo">
      <!-- <template #toolbar>
        <a-button type="primary" @click="handleCreate">导出</a-button>
      </template> -->
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
import { reactive } from 'vue';

import { BasicTable, useTable } from '@/components/Table';
import { PageWrapper } from '@/components/Page';
import RecordTree from './RecordTree.vue';

import { useModal } from '@/components/Modal';
import AccountModal from './AccountModal.vue';

import { columns, searchFormSchema } from './record.data';
import { getDeviceMeasureRecordPage } from '@/api/collection/device/index';

defineOptions({ name: 'RecordManagement' });

const [registerModal, { openModal }] = useModal();
const searchInfo = reactive<Recordable>({ deviceId: 0 });
const [registerTable, { reload, updateTableDataRecord, setPagination }] = useTable({
  title: '',
  api: getDeviceMeasureRecordPage,
  rowKey: 'id',
  columns,
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
    autoSubmitOnEnter: true,
  },
  searchInfo,
  useSearchForm: true,
  showTableSetting: false,
  bordered: true,
  beforeFetch(info) { // 手动处理time属性
    if (info.time) {
      const timeparams = {
        startTime: info.time[0].toString(),
        endTime: info.time[1].toString()
      }
      delete info.time;
      return { ...info, ...timeparams }
    }
  },
  // handleSearchInfoFn(info) {
  //   return {
  //     startTime: info.time[0].toString(),
  //     endTime: info.time[1].toString()
  //   };
  // },
  // actionColumn: {
  //   width: 120,
  //   title: '操作',
  //   dataIndex: 'action',
  //   // slots: { customRender: 'action' },
  // },
});

function handleCreate() {
  openModal(true, {
    isUpdate: false,
  });
}

function handleEdit(record: Recordable) {
  console.log(record);
  openModal(true, {
    record,
    isUpdate: true,
  });
}

function handleSuccess({ isUpdate, values }) {
  if (isUpdate) {
    // 演示不刷新表格直接更新内部数据。
    // 注意：updateTableDataRecord要求表格的rowKey属性为string并且存在于每一行的record的keys中
    const result = updateTableDataRecord(values.id, values);
    console.log(result);
  } else {
    reload();
  }
}

function handleSelect(deviceId = 0) {
  searchInfo.deviceId = deviceId;
  setPagination({ current: 1 })
  reload();
}
</script>
